{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名  刘栋\n",
    "+ 学号  0160617"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "四十八年过 浮华浪掷岁月摧折\n",
      "七十三篇多情旧梦外 流云过\n",
      "孤城云散去 万里皆留白\n",
      "月沉 星埋\n",
      "笔尖光影动 醉后侠气来\n",
      "九州山河开\n",
      "\n",
      "\n",
      "剑点三分桃花颜色\n",
      "对月堪独酌\n",
      "一夜飞舟渡星河\n",
      "谁道这 春风酩酊眼波\n",
      "知己知交庸人闲客 几人堪知我\n",
      "一晌风雪满关河\n",
      "又数过 一十七朵梅花落\n",
      "\n",
      "\n",
      "江湖在酒中杯中或是他心中\n",
      "侠客已远走梦中或是残页中\n",
      "身前天涯洪炉浩浩苍穹\n",
      "等谁 相逢\n",
      "身后万丈人海荒芜成空\n",
      "转眼也成空\n",
      "\n",
      "\n",
      "剑点三分桃花颜色\n",
      "对月堪独酌\n",
      "一夜飞舟渡星河\n",
      "谁道这 春风酩酊眼波\n",
      "知己知交庸人闲客 几人堪知我\n",
      "一晌风雪满关河\n",
      "又数过 一十七朵梅花落\n",
      "\n",
      "\n",
      "狂歌豪酒盛世长烽 天地来入梦\n",
      "敢化人间冰雪重\n",
      "谁道这 半生痴绝与共\n",
      "纸上行间故人无踪 两忘烟尘中\n",
      "只将生死笑长空\n",
      "擦肩去 刀光剑影一场梦\n",
      "\n",
      "心未动 亦未相逢\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def lyric():\n",
    "    print(\"\\n剑点三分桃花颜色\\n对月堪独酌\\n一夜飞舟渡星河\\n谁道这 春风酩酊眼波\\n知己知交庸人闲客 几人堪知我\\n一晌风雪满关河\\n又数过 一十七朵梅花落\\n\")\n",
    "print(\"四十八年过 浮华浪掷岁月摧折\\n七十三篇多情旧梦外 流云过\\n孤城云散去 万里皆留白\\n月沉 星埋\\n笔尖光影动 醉后侠气来\\n九州山河开\\n\")\n",
    "lyric()\n",
    "print(\"\\n江湖在酒中杯中或是他心中\\n侠客已远走梦中或是残页中\\n身前天涯洪炉浩浩苍穹\\n等谁 相逢\\n身后万丈人海荒芜成空\\n转眼也成空\\n\")\n",
    "lyric()\n",
    "print(\"\\n狂歌豪酒盛世长烽 天地来入梦\\n敢化人间冰雪重\\n谁道这 半生痴绝与共\\n纸上行间故人无踪 两忘烟尘中\\n只将生死笑长空\\n擦肩去 刀光剑影一场梦\\n\\n心未动 亦未相逢\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "数据如：[99, 5, 8, 4, 9, 6, 20, 88, 2, 11]\n",
      "均值是:25.2\n",
      "样本方差是:1194.16\n",
      "\n",
      "数据如：[5, 81, 47, 19, 25, 3, 31, 14, 5, 6, 71, 61]\n",
      "均值是:30.666666666666668\n",
      "样本方差是:707.0555555555554\n"
     ]
    }
   ],
   "source": [
    "def st(y):\n",
    "    print(\"\\n数据如：%s\"%y)\n",
    "    n=0\n",
    "    m=0\n",
    "    for value in y:\n",
    "        n+=1\n",
    "        m+=value\n",
    "    ave=m/n\n",
    "    print(\"均值是:%s\" % ave)\n",
    "    sumsquare=0\n",
    "    for value in y:\n",
    "        sumsquare+=value**2\n",
    "    var=sumsquare/n-ave**2\n",
    "    print(\"样本方差是:%s\" % var)\n",
    "st([99,5,8,4,9,6,20,88,2,11])\n",
    "st([5,81,47,19,25,3,31,14,5,6,71,61])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "数据如：[99, 5, 8, 4, 9, 6, 20, 88, 2, 11]\n",
      "均值是:25.2\n",
      "输入0时输出方差；输入非0时输出标准差\n",
      "样本标准差是:34.55662020510687\n",
      "\n",
      "数据如：[5, 81, 47, 19, 25, 3, 31, 14, 5, 6, 71, 61]\n",
      "均值是:30.666666666666668\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "def st(y):\n",
    "    print(\"\\n数据如：%s\"%y)\n",
    "    n=0\n",
    "    m=0\n",
    "    for value in y:\n",
    "        n+=1\n",
    "        m+=value\n",
    "    ave=m/n\n",
    "    print(\"均值是:%s\" % ave)\n",
    "    sumsquare=0\n",
    "    for value in y:\n",
    "        sumsquare+=value**2\n",
    "    var=sumsquare/n-ave**2\n",
    "    t=input(\"输入0时输出方差；输入非0时输出标准差\")\n",
    "    if t=='0':\n",
    "        print(\"样本方差是:%s\" % var)\n",
    "    else:\n",
    "        vars=math.sqrt(var)\n",
    "        print(\"样本标准差是:%s\" % vars)\n",
    "st([99,5,8,4,9,6,20,88,2,11])\n",
    "st([5,81,47,19,25,3,31,14,5,6,71,61])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "数据如：[99, 5, 8, 4, 9, 6, 20, 88, 2, 11]\n",
      "均值是:25.2\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "def st(y):\n",
    "    print(\"\\n数据如：%s\"%y)\n",
    "    n=0\n",
    "    m=0\n",
    "    max=0\n",
    "    min=99999\n",
    "    for value in y:\n",
    "        n+=1\n",
    "        m+=value\n",
    "    ave=m/n\n",
    "    print(\"均值是:%s\" % ave)\n",
    "    sumsquare=0\n",
    "    for value in y:\n",
    "        sumsquare+=value**2\n",
    "    var=sumsquare/n-ave**2\n",
    "    t=input(\"输入0时输出其他三个统计量；输入非0时不输出\")\n",
    "    for value in y:\n",
    "        if value>max:\n",
    "            max=value\n",
    "        if value<min:\n",
    "            min=value\n",
    "    nsquare=math.sqrt(n)  \n",
    "    if t=='0':\n",
    "        print('最大值是%s'%max)\n",
    "        print('最小值是%s'%min)\n",
    "        print(\"均值标准误为%s\" % (var/nsquare))  \n",
    "st([99,5,8,4,9,6,20,88,2,11])\n",
    "st([5,81,47,19,25,3,31,14,5,6,71,61])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
